<?php
class osa_db_model{
	
	var $db = null;

    /**
     * osa_db_model constructor.
     */
	public function __construct(){
		$this->db = new osa_db(OSA_MYSQL_CONN_DNS,OSA_MYSQL_CONN_USER,OSA_MYSQL_CONN_PASSWD,OSA_MYSQL_CONN_CHARSET,OSA_MYSQL_CONN_PRIFIX);
	}

    /**
     * 通过ID获取一行数据
     */
	public function getRowById($TABLENAME,$ID){
	    $sql = "SELECT * FROM ".$TABLENAME." WHERE id=".$ID ;
	    $result = $this->db->queryFetchAllAssoc($sql) ;
        return $result ;
    }

    /**
     * 通过ID获取一行数据中的某一字段
     */
    public function getColById($TABLENAME,$ID){

    }

    /**
     * 根据某个字段获取多行数据
     */
    public function getRowByItem($TABLENAME,$ITEMNAME,$ITEMVALUE){

    }

    /**
     * 根据某个字段获取多行中多列数据
     */
    public function getColByItem($TABLENAME,$ITEMNAME,$ITEMVALUE){

    }

    /**
     * 根据某个字段获取多行中多列数据
     */
    public function getAll($TABLENAME,$para){

        $sql = "SELECT * FROM ".$TABLENAME  ;

        if( isset($para)){
            $sql_str = " WHERE id>0 " ;
            foreach($para as $v=>$key){
                $sql_str =  $sql_str." AND ".$v."=".$key ;
            }
            $sql =$sql.$sql_str ;
        }

        $result = $this->db->queryFetchAllAssoc($sql) ;
        return $result ;
    }

    /**
     * 根据表名插入数据
     */
    public function insertData($TABLENAME,$para){

        $sql_key = " id " ;
        $sql_value = " 0 " ;

        if( isset($para)){
            foreach($para as $v=>$key){
                $sql_key = $sql_key.",".$v ;
                $sql_value = $sql_value.",".$key ;
            }
        }
        $sql = "INSERT INTO ".$TABLENAME ." (".$sql_key.") ". " VALUES(".$sql_value.")" ;

       $ins = $this->db->query($sql);

        return $ins ;
    }

    /**
     * 根据表名修改数据
     */
    public function updateData($TABLENAME,$para){

    }

    /**
     * 根据表名删除数据
     */
    public function deleteData($TABLENAME,$para){

        $sql = "DELETE  FROM ".$TABLENAME  ;

        if( isset($para)){
            $sql_str = " WHERE id>0 " ;
            foreach($para as $v=>$key){
                $sql_str =  $sql_str." AND ".$v."=".$key ;
            }
            $sql =$sql.$sql_str ;
        }

        $result = $this->db->query($sql) ;
        return $result ;

    }

    /**
     * 根据表名搜索数据
     */
    public function searchData($TABLENAME,$para){

    }



    /**
     * 添加系统日志
     */
    public function saveSysLog($type=1 ,$title ,$text ,$username){
        $time = date("Y-m-d H:i:s",time());
        $sql = "INSERT INTO osa_syslog(oTypeid,oUserName,oLogTitle,oLogText,oLogAddTime) VALUES ('$type','$username','$title','$text','$time')";
        $this->db->exec($sql);
    }
    /**
     * 通过用户id获取用户的权限列表
     */
    public function getRoleByUid($id){
        $sql = "select oPerArr from osa_roles as A right join osa_users as B on A.id = B.oRoleid where B.id =".$id;
        $rs = $this->db->queryFetchAllAssoc($sql);
        return $rs[0]['oPerArr'];
    }

}